/**
 * 短信
 */
import api from '../../tools/api'
import {SUCCESS_STATUS} from '../../config/status'
import promise from '../../tools/promise'
import {
  SMS_LIST,
  SMS_LOG_LIST,
  SMS_INFO
} from '../mutation-types'

const state = {
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  smsList: [],
  logList: [],
  smsInfo: {},
  saveData: {}
}

const mutations = {
  [SMS_LIST] (state, payload) {
    state.smsList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [SMS_LOG_LIST] (state, payload) {
    state.logList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [SMS_INFO] (state, payload) {
    state.smsInfo = payload
  }
}

const actions = {
  // 获取短信列表
  async smsList ({commit}, payload) {
    let listRes = await api('serversms/sms/Sms/smsList', payload)
    if (SUCCESS_STATUS === listRes.status) {
      commit(SMS_LIST, listRes.res)
    }
  },
  // 删除短信
  async smsDel ({commit}, payload) {
    let delRes = await api('serversms/sms/Sms/delOneSms', payload)
    return promise(delRes)
  },
  // 获取短信日志列表
  async logList ({commit}, payload) {
    let listRes = await api('serversms/sms/Sms/logList', payload)
    commit(SMS_LOG_LIST, listRes.res)
  },
  // 添加短信模板
  async smsAdd ({commit}, payload) {
    let AddRes = await api('serversms/sms/Sms/addSms', payload)
    return promise(AddRes)
  },
  // 获取短信模板详情
  async smsInfo ({commit}, payload) {
    let infoRes = await api('serversms/sms/Sms/smsInfo', payload)
    if (SUCCESS_STATUS === infoRes.status) {
      commit(SMS_INFO, infoRes.res)
    }
  },
  // 编辑短信模板
  async smsEdit ({commit}, payload) {
    let editRes = await api('serversms/sms/Sms/editSms', payload)
    return promise(editRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
